﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BL.Service;
using BL.Entity;

namespace LTO
{
    /// <summary>
    /// Interaction logic for viewUnder1.xaml
    /// </summary>
    public partial class viewUnder1 : Window
    {
        tbl_NumberServer Numberservice = new tbl_NumberServer();
        tbl_PayService PayService = new tbl_PayService();
        List<ShowNumberDTO> show;
        List<ShowNumberDTO> show1;
        List<ShowNumberDTO> show2;
        Pay limit;
        decimal LimitNum = 0;
        decimal minSt = 0;
        decimal minEn = 0;
        string NumberType;
        bool IsMinToMax;
        bool IsShowOver;
        bool IsShowDummy;

        HotKey _HotKey_Esc;
        HotKey _HotKey_F1;
        HotKey _hotkey_F4;
        HotKey _hotKey_F3;
        HotKey _HotKey_Print;
        HotKey _HotKey_F2;
        HotKey _HotKey_DeleteNum;

        public viewUnder1()
        {
            InitializeComponent();

            Newload();
            this.Title = "วิ่ง ล่าง";
             _HotKey_Esc = new HotKey(Key.Escape, KeyModifier.None, OnCloseWindows);
             _HotKey_F1 = new HotKey(Key.F1, KeyModifier.None, Newload);
             _hotkey_F4 = new HotKey(Key.F4, KeyModifier.None, OnHotKeyShowDummy);
             _hotKey_F3 = new HotKey(Key.F3, KeyModifier.None, OnHotKeyShowOverLimit);
             _HotKey_Print = new HotKey(Key.F9, KeyModifier.None, PrintReport);
             _HotKey_F2 = new HotKey(Key.F2, KeyModifier.None, ShowMinToMax);
             _HotKey_DeleteNum = new HotKey(Key.Subtract, KeyModifier.None, DeleteNum);
        }
        public void UnregisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Unregister();
            }
        }
        public void RegisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Register();
            }
        }
        protected void DeleteNum(HotKey h)
        {
            List<ShowNumberDTO> tempdel = new List<ShowNumberDTO>();
            tempdel.AddRange(show);
            tempdel.AddRange(show1);
            tempdel.AddRange(show2);
            UnregisterDelete();
            deleteNUm deleteWin = new deleteNUm(tempdel, EnumLocal.NumberType.Under1);
            deleteWin.Owner = this;
            deleteWin.Show();
        }
        private void PrintReport(HotKey hotKey)
        {
            List<ShowNumberDTO> print_temp = new List<ShowNumberDTO>();
            print_temp.AddRange(show);
            print_temp.AddRange(show1);
            print_temp.AddRange(show2);

            Gobalsource.PrintReport(print_temp, EnumLocal.NumberType.Under1, LimitNum);

            Newload();
            
        }
        protected void ShowMinToMax(HotKey h)
        {
            IsMinToMax = !IsMinToMax;
            LoadData();
        }
        protected void OnHotKeyShowDummy(HotKey h)
        {
            IsShowDummy = !IsShowDummy;
            LoadData();
        }
        protected void OnHotKeyShowOverLimit(HotKey h)
        {
            IsShowOver = !IsShowOver;
            LoadData();
        }
        private void Newload(HotKey h)
        {
            Newload();
        }
        public void Newload()
        {
            limit = PayService.GetLimit(EnumLocal.NumberType.Under1);

            LimitNum = limit.LimitBuy;
            minSt = limit.MinStart;
            minEn = limit.MinEnd;
           
            if (this.Owner != null)
            {
                ClientMain main = (ClientMain)this.Owner;
                main.ReloadSceen();
            }
            GetNumberShow();
        }
        private void OnCloseWindows(HotKey hotKey)
        {
            (Owner as ClientMain).UpdateData();
            this.Close();
        }
        public void GetNumberShow()
        {
            List<NumberDTO> dbNUmber = new List<NumberDTO>();
            
            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Under1);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Under1);
            show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Under, LimitNum, minSt, minEn);

            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Under1Decimal);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Under1Decimal);
            show1 = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Under, LimitNum, minSt, minEn);

            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Under1unit);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Under1unit);
            show2 = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Under, LimitNum, minSt, minEn);

            //btnCut.IsEnabled = show.Count(c => c.IsOver == true) >= 1 || show1.Count(c => c.IsOver == true) >= 1 || show2.Count(c => c.IsOver == true) >= 1;
            labSum.Content = String.Format("ยอดขายรวม : {0}", show.Sum(s => s.Price) + show1.Sum(s => s.Price) + show2.Sum(s => s.Price));
            LoadData();

        }
        private void LoadData()
        {
            List<ShowNumberDTO> show_temp = new List<ShowNumberDTO>();
            List<ShowNumberDTO> show1_temp = new List<ShowNumberDTO>();
            List<ShowNumberDTO> show2_temp = new List<ShowNumberDTO>();
          
            foreach (ShowNumberDTO showitem in show)
            {
                show_temp.Add(showitem.Clone());
            }
            foreach (ShowNumberDTO showitem in show1)
            {
                show1_temp.Add(showitem.Clone());
            }
            foreach (ShowNumberDTO showitem in show2)
            {
                show2_temp.Add(showitem.Clone());
            }
         
            

            if (!IsShowDummy)
            {
                show_temp = show_temp.Where(w => w.IsZero == false).ToList();
                show1_temp = show1_temp.Where(w => w.IsZero == false).ToList();
                show2_temp = show2_temp.Where(w => w.IsZero == false).ToList();
             
            }
            if (IsShowOver)
            {
                show_temp = show_temp.Where(w => w.IsOver == true).ToList();
                show1_temp = show1_temp.Where(w => w.IsOver == true).ToList();
                show2_temp = show2_temp.Where(w => w.IsOver == true).ToList();

                show_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
                show1_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
                show2_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
              
            }
            if (IsMinToMax)
            {
                show_temp = show_temp.OrderBy(n => n.Price).ToList();
                show1_temp = show1_temp.OrderBy(n => n.Price).ToList();
                show2_temp = show2_temp.OrderBy(n => n.Price).ToList();
               
            }
            else
            {
                show_temp = show_temp.OrderByDescending(n => n.Price).ToList();
                show1_temp = show1_temp.OrderByDescending(n => n.Price).ToList();
                show2_temp = show2_temp.OrderByDescending(n => n.Price).ToList();
                
            }


            listBoxPage1.ItemsSource = show_temp;
            listBoxPage2.ItemsSource = show1_temp;
            listBoxPage3.ItemsSource = show2_temp;
            labSum.Content = String.Format("{0:#,#}", show.Sum(s => s.Price) + show1.Sum(s => s.Price) + show2.Sum(s => s.Price));
          

        }
        private void btnCut_Click(object sender, RoutedEventArgs e)
        {
            tbl_ExportService exportService = new tbl_ExportService();
            exportService.ExportNumber(LimitNum, EnumLocal.NumberType.Under1, Gobalsource.UserNameLogin);
            this.GetNumberShow();
        }
     

        private void listBoxPage1_KeyDown_1(object sender, KeyEventArgs e)
        {
            ShowNumberDTO select;
            switch (e.Key)
            {

                case Key.OemMinus:
                     select = listBoxPage1.SelectedItem as ShowNumberDTO;
                    Numberservice.DeleteNumber(select, EnumLocal.NumberType.Under1, Gobalsource.UserNameLogin);
                    this.GetNumberShow();
                    break;
                case Key.Subtract:
                        select = listBoxPage1.SelectedItem as ShowNumberDTO;
                    Numberservice.DeleteNumber(select, EnumLocal.NumberType.Under1, Gobalsource.UserNameLogin);
                    this.GetNumberShow();
                    break;
            }
        }

        private void listBoxPage2_KeyDown(object sender, KeyEventArgs e)
        {
            ShowNumberDTO select;
            switch (e.Key)
            {

                //case Key.OemMinus:
                //     select = listBoxPage1.SelectedItem as ShowNumberDTO;
                //    Numberservice.DeleteNumber(select, EnumLocal.NumberType.Under1Decimal, Gobalsource.UserNameLogin);
                //    this.GetNumberShow();
                //    break;
                //case Key.Subtract:
                //     select = listBoxPage1.SelectedItem as ShowNumberDTO;
                //    Numberservice.DeleteNumber(select, EnumLocal.NumberType.Under1Decimal, Gobalsource.UserNameLogin);
                //    this.GetNumberShow();
                //    break;

            }
        }

        private void listBoxPage3_KeyDown(object sender, KeyEventArgs e)
        {
            ShowNumberDTO select;
            switch (e.Key)
            {

                //case Key.OemMinus:
                //     select = listBoxPage1.SelectedItem as ShowNumberDTO;
                //    Numberservice.DeleteNumber(select, EnumLocal.NumberType.Under1unit, Gobalsource.UserNameLogin);
                //    this.GetNumberShow();
                //    break;
                //case Key.Subtract:
                //        select = listBoxPage1.SelectedItem as ShowNumberDTO;
                //    Numberservice.DeleteNumber(select, EnumLocal.NumberType.Under1unit, Gobalsource.UserNameLogin);
                //    this.GetNumberShow();
                //    break;

            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            (Owner as ClientMain).UpdateData(); 
        }

    }
}
